import {spiralPrint} from "./utils"

/**
 * 添加点
 *
 * @param graph 图对象
 * @param nodeCfg 点的配置信息
 * @param nodeIndex 点的ID
 * @param nodePosition 初始位置
 */
export const addNode = (graph, nodeCfg, nodeIndex, nodePosition) => {
    const addStep = 80;
    let nodeArray = spiralPrint(nodeIndex);
    let currentIndex = nodeIndex;
    let minNode = {
        x: 0,
        y: 0
    }
    let maxNode = {
        x: 0,
        y: 0
    }

    // console.log('当前索引', currentIndex)
    nodeArray.forEach((item, index) => {
        item.forEach((subItem, subIndex) => {
            if (currentIndex === subItem) {
                maxNode.x = index;
                maxNode.y = subIndex;
            }
            if (1 === subItem) {
                minNode.x = index;
                minNode.y = subIndex;
            }
        })
    })

    let currentPosition = {
        x: nodePosition.x + (maxNode.x - minNode.x) * addStep,
        y: nodePosition.y - (maxNode.y - minNode.y) * addStep
    }

    let node = {
        id: nodeCfg.id,
        label: nodeCfg.label,
        x: currentPosition.x,
        y: currentPosition.y,
        size: [40, 40],
        img: nodeCfg.img,
        type: "person-node"
    }

    graph.addItem("node", node, true)
}